package com.lzx.utils;

import com.csvreader.CsvWriter;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;


public class ExcelUtil {


    private static void isChartPathExist(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public synchronized static void createCSV( List<String>title,List<String> content,List<String>channel,String filepath) {

        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filepath,',', Charset.forName("GBK"));
            //CsvWriter csvWriter = new CsvWriter(filePath);
            // 写表头
            String[] headers = {"title","content","channelName"};
            csvWriter.writeRecord(headers);
            for (int i=0;i<title.size();i++)
            {
                String[] con={title.get(i),content.get(i),channel.get(i)};
                csvWriter.writeRecord(con);
            }
            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public synchronized static void createExcel( List<String>title,List<String> content,List<String>channel,String filepath) {
        // TODO Auto-generated method stub
        try {
            //创建HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook wb = new HSSFWorkbook();
            // 建立新的sheet对象（excel的表单）
            HSSFSheet sheet = wb.createSheet("sheet1");
            // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
            HSSFRow row0 = sheet.createRow(0);
            // 添加表头
            row0.createCell(0).setCellValue("title");
            row0.createCell(1).setCellValue("content");
            row0.createCell(2).setCellValue("channelName");
            int row = 1;
            //添加表中内容
            for (int i=0;i<content.size();i++) {
                HSSFRow newrow = sheet.createRow(row);
                newrow.createCell(1).setCellValue(content.get(i));
                newrow.createCell(0).setCellValue(title.get(i));
                newrow.createCell(2).setCellValue(channel.get(i));
                row++;
            }
            //判断是否存在目录. 不存在则创建
//            isChartPathExist(filepath);
            //输出Excel文件1
            File file = new File(filepath);
            FileOutputStream output = new FileOutputStream(file);
            wb.write(output);//写入磁盘
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  static void writeExcel(List<List<String>> all, String[] types, String name,List<List<String>>titles) {
        // TODO Auto-generated method stub
        String  filepath = "D:\\User\\新闻分类";
        String filename =name + ".xlsx";
        try {
            //创建HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook wb = new HSSFWorkbook();
            // 建立新的sheet对象（excel的表单）
            for (int i=0;i<types.length;i++) {
                HSSFSheet sheet = wb.createSheet(types[i]);
                // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
                HSSFRow row0 = sheet.createRow(0);
                // 添加表头
                row0.createCell(0).setCellValue("content");
                row0.createCell(1).setCellValue("channelName");
                row0.createCell(2).setCellValue("title");
                int row = 1;
                //添加表中内容
                List<String> list = all.get(i);
                List<String> title = titles.get(i);
                for (int j=0; j<list.size(); j++) {
                    String content = list.get(j);
                    HSSFRow newrow = sheet.createRow(row);
                    if (content.length()>32700) {
                        System.out.println(content);
                        continue;
                    }
                    newrow.createCell(0).setCellValue(content);
                    newrow.createCell(1).setCellValue(types[i]);
                    newrow.createCell(2).setCellValue(title.get(j));
                    row++;
                }
            }
            //判断是否存在目录. 不存在则创建
            isChartPathExist(filepath);
            //输出Excel文件1
            File file = new File(filepath +"\\"+ filename);
            FileOutputStream output = new FileOutputStream(file);
            wb.write(output);//写入磁盘
            output.flush();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
